/**
 * 工具扩展类
 * wuyang
 *
 * */
layui.define(["jquery", "layer", 'table', 'soulTable'], function (exports) {
    var model_name = "myTool";
    var $ = layui.jquery,
        layer = layui.layer,
        table = layui.table;

    var obj = {
        /////树型点击高量显示
        TreeSelect: function (o) {
            $(document).find(".tree-selectd").removeClass('tree-selectd');
            $(o.elem).find(".layui-tree-txt").first().addClass("tree-selectd");
        }
        /////树型点击高量显示
        , TreeSelectInit: function (ids) {
            $("div [data-id]").each(function (obj) {
                if (this.getAttribute("data-id") == ids) {
                    $(this).find(".layui-tree-txt").addClass("tree-selectd");
                }
            })
        }
        //单选处理函数
        , doSingleSelect: function (checkStatus, opt, callback) {
            var data = checkStatus.data;
            if (data.length != 1) {
                layer.msg(opt.msg ? opt.msg : '请选择一条记录');
            } else {
                callback(data, data[0][opt.key]);
            }
        }
        //多选处理函数
        , doMoreSelect: function (checkStatus, opt, callback) {
            var data = checkStatus.data;
            if (data.length == 0) {
                layer.msg(opt.msg ? opt.msg : '请选择操作记录');
            } else {
                var keys = "";
                data.forEach(function (item, index) {
                    keys += item[opt.key];
                    if (index < data.length - 1)keys += ",";
                });
                callback(data, keys);
            }
        }
        , post: function (urls, datas, callback) {
            $.ajax({
                type: 'POST',
                url: urls,
                async: false,
                data: datas,
                success: function (results) {
                    if (results.code == 0) {
                        top.layer.alert(results.msg, {offset: '150px'}, function (index1) {
                            callback.call(results);
                            top.layer.close(index1);
                        });
                    } else {
                        top.layer.msg(results.msg);
                    }
                },
                error: function (return_data) {
                    top.layer.msg('操作失败');
                },
                dataType: "json"
            });
        }
        , postJson: function (urls, datas, callback) {
            layui.layer.load(2, {time: 10 * 1000});
            $.ajax({
                type: 'POST',
                url: urls,
                async: false,
                data: JSON.stringify(datas),
                contentType: 'application/json',
                success: function (results) {
                    layui.layer.closeAll('loading');
                    if (results.code == 0) {
                        top.layer.alert(results.msg, {offset: '150px'}, function (index1) {
                            callback(results);
                            top.layer.close(index1);
                        });
                    } else if (results.code == 999) {
                        top.layer.msg(results.data.errors);
                        $("#" + results.data.token_name).val(results.data.token_value);
                        $(results.data.fields).focus();
                        $(results.data.fields).addClass("layui-form-danger");
                    } else {
                        top.layer.msg(results.msg);
                    }
                },
                error: function (return_data) {
                    console.info(urls);
                    console.info(return_data.status);
                    console.info(return_data.statusText);
                    top.layer.msg('操作失败[' + return_data.status + "]");
                },
                dataType: "json"
            });
        }
        , postAlert: function (urls, datas, callback) {
            $.ajax({
                type: 'POST',
                url: urls,
                async: true,
                data: datas,
                success: function (results) {
                    layer.closeAll('loading');
                    parent.layer.closeAll('loading');
                    top.layer.closeAll('loading');
                    if (results.code == 0) {
                        top.layer.alert(results.msg, {offset: '150px'}, function (index1) {
                            callback.call(results);
                            top.layer.close(index1);
                        });
                    } else {
                        top.layer.msg(results.msg);
                    }
                },
                error: function (return_data) {
                    layer.closeAll('loading');
                    parent.layer.closeAll('loading');
                    top.layer.closeAll('loading');
                    top.layer.msg('操作失败');
                    callback.call(results);
                },
                dataType: "json"
            });
        }
        , get: function (urls, datas, callback) {
            $.ajax({
                type: 'GET',
                url: urls,
                async: false,
                data: datas,
                success: function (results) {
                    layer.closeAll('loading');
                    parent.layer.closeAll('loading');
                    top.layer.closeAll('loading');
                    if (results.code == 0) {
                        top.layer.alert(results.msg, {offset: '150px'}, function (index1) {
                            top.layer.close(index1);
                            callback.call(results);
                        });
                    } else {
                        top.layer.msg(results.msg);
                    }
                },
                error: function (return_data) {
                    top.layer.msg('操作失败');
                },
                dataType: "json"
            });
        }
        , postMsg: function (urls, datas, callback) {
            layer.load();
            $.ajax({
                type: 'POST',
                url: urls,
                async: true,
                data: datas,
                success: function (results) {
                    layer.closeAll('loading');
                    parent.layer.closeAll('loading');
                    top.layer.closeAll('loading');
                    if (results.code == 0) {
                        top.layer.msg(results.msg);
                        callback.call(results.msg);
                    } else {
                        top.layer.msg(results.msg);
                        callback.call(results.msg);
                    }
                    return false;
                },
                error: function (return_data) {
                    top.layer.msg(return_data.msg);
                    callback.call(return_data.msg);
                    return false;
                },
                dataType: "json"
            });
        }
        , toolbar: function (obj, obj2, events) {
            if ($(obj).hasClass("layui-btn-disabled"))return;
            if (obj2.event == 'CloseWindow') {
                return parent.layer.closeAll();
            } else if (obj2.event == 'ReloadTable') {
                return table.reload(obj2.config.id);
            } else if (obj2.event == 'export') {
                return false;
            } else if (typeof(events[obj2.event]) == 'function') {
                return events[obj2.event](obj2);
            } else {
                // console.error("function not fund:" + obj2.event);
                return false;
            }
        }
        , tool: function (obj, obj2, events) {
            if ($(obj).hasClass("layui-btn-disabled"))return;
            if (typeof(events[obj2.event]) == 'function') {
                return events[obj2.event](obj2.data);
            } else {
                return false;
            }
        }
        , reload: function () {
            /** tips  **/
            $('*[lay-tips]').off().on('mouseenter', function () {
                const content = $(this).attr('lay-tips');
                const options = (eval('(' + $(this).attr('lay-tips-option') + ')')) || {tips: [2, '#6B7280']};
                this.index = layer.tips(content, this, options);
            }).on('mouseleave', function () {
                layer.close(this.index);
            });
        }
    }

    exports(model_name, obj);
});